package com.orangelife.mobile.common.activity;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;

import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.thirdsdk.baidu.BaiduLocationActivity;
import com.thirdsdk.baidu.MapReceiver;
import com.thirdsdk.baidu.MyLocationListener;
/**
 * 地图基类
 * @author Administrator
 *
 */
public abstract class BaseMapActivity extends BaiduLocationActivity{
	/**用来发起定位，添加取消监听*/
	protected LocationClientOption option;
	protected MapReceiver receiver;
    protected LocationClient mLocClient;
    protected MyLocationListener myListener;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		registerSDK();
		initMyLocation();
	}

	/**
     * 注册 SDK 广播监听者
     */
	private void registerSDK() {
		IntentFilter iFilter = new IntentFilter();
		iFilter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR);
		iFilter.addAction(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR);
	}
    /**
     * 设置定位的相关配置
     */
	private void initMyLocation() {
		option = new LocationClientOption();
		option.setOpenGps(true);// 打开gps
		option.setCoorType("bd09ll");// 设置坐标类型
		option.setScanSpan(60000);
		option.setIsNeedAddress(true);
		option.setProdName("orangelife");
		option.setAddrType("all");
		option.setLocationMode(com.baidu.location.LocationClientOption.LocationMode.Hight_Accuracy);// 设置定位模式		
		myListener = initListener(myListener);
		if(myListener!=null) {
			/** 定位初始化 */
			mLocClient = new LocationClient(this);
			mLocClient.registerLocationListener(myListener);
			mLocClient.setLocOption(option);
			mLocClient.start();
		}
	}

	/**
	 * 注册广播接收
	 */
	protected void registerReceiver(Handler handler,int what) {
		if(!isConnected()){
			return;
		}
		receiver = new MapReceiver(handler,what);
		IntentFilter filter = new IntentFilter();
		filter.addAction("MapService");
		registerReceiver(receiver, filter);
	}

	public abstract MyLocationListener initListener(MyLocationListener myListener);

	@Override
	protected void onPause() {
		super.onPause();
		unregisterReceiver(receiver);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(mLocClient!=null) {
			mLocClient.stop();
		}
	}
}
